package com.pkk.alert.common.util;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.pkk.alert.common.dto.FeiGeReqDataVo;
import java.util.HashMap;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @description: 报警工具类
 * @author: peikunkun
 * @create: 2019-07-05 09:51
 **/
public class PenguinAlertUtil {


  public static void main(String[] args) {
    System.out.println(getNowToLastSecordDayOfMonth());
    String templateContent = "{{first}}\n"
        + "通知内容：{{keyword1}}\n"
        + "通知人：{{keyword2}}\n"
        + "通知时间：{{keyword3}}\n"
        + "{{remark}}";
    String[] values = new String[]{"1", "2", "3", "4", "5"};
    System.out.println(JSONObject.toJSONString(parserTemplateContent(templateContent, values)));
  }


  /**
   * @Description: 获取当前时间举例本月最后一天的秒数
   * @Param: []
   * @return: java.lang.Long
   * @Author: peikunkun
   * @Date: 2019/7/5 0005 上午 9:58
   */
  public static Long getNowToLastSecordDayOfMonth() {
    final DateTime now = DateTime.now();
    final DateTime dateTime = DateUtil.endOfMonth(now);
    return now.between(dateTime, DateUnit.SECOND);
  }


  /**
   * @Description: 解析模板文本内容
   * @Param: [templateContent]
   * @Author: peikunkun
   * @Date: 2019/7/5 0005 上午 11:54
   */
  public static HashMap<String, FeiGeReqDataVo> parserTemplateContent(String templateContent, String... param) {
    Optional.ofNullable(templateContent).orElseThrow(() -> new IllegalArgumentException("模板内容不能为空"));
    String pre = "\\{\\{([\\s\\S]*?)}}";
    Pattern pattern = Pattern.compile(pre);
    Matcher matcher = pattern.matcher(templateContent);

    HashMap<String, FeiGeReqDataVo> paramFinal = new HashMap<>();
    int i = 0;
    while (matcher.find()) {
      paramFinal.put(matcher.group(1), FeiGeReqDataVo.builder().value(param[i]).build());
      i++;
    }
    return paramFinal;
  }

}
